#ifndef HASHTABLE_CPP
#define HASHTABLE_CPP_

#include "Hashtable.h"
#include <string.h>
#include <iostream>
#include <math.h>

#define Hashnumber 1024
#define A int(pow(2,10))*19
#define B 1991

Hashtable::Hashtable()
{
  head = new Hashnode[Hashnumber];
};

Hashtable::Hashtable(Hashnode* _head)
{
  head = _head;
};

unsigned int Hashkey(char* _word)
{
  unsigned int tmp = 0;
  for( ; *_word != '\0'; _word++)
    tmp = B*tmp + *_word;
  unsigned key = ((A*tmp)%int(pow(2,64)))>>54;
  return key;
};

void Hashtable::insert(char* _word)
{
  unsigned int key = Hashkey(_word);
  if((head+key)->isempty)
    {
      *(head+key) = Hashnode(_word);
    }
  else
    {
      Hashnode* p = head+key;
      if (strcmp(_word,p->get_word()) == 0)
	{
            p->set_count(p->get_count()+1);
            return;
        }
      do
	{
	   p = p->get_next();
	   if (strcmp(_word,p->get_word()) == 0)
	    {
	      p->set_count(p->get_count()+1);
	      return;
	    }
	}while(p->get_next() != p->nil);
      p->set_next(new Hashnode(_word));
      return;
    }
};

Hashnode* Hashtable::search(char* _word) const
{
  unsigned int key = Hashkey(_word);
  Hashnode* p = head+key;
  for( ; p != p->nil ; p=p->get_next())
    {
      if(strcmp(_word, p->get_word()) == 0)
	{
	  std::cout << "Found!" << std::endl;
	  return p;
	}
    }
  std::cout << "Not Found!" << std::endl;
  return p->nil;
};

#endif
